package com.xframe.common.core.service;

import com.xframe.domain.CommandModel;
import com.xframe.domain.sys.SysCommand;
import com.xframe.common.core.dto.ErrorCode;
import com.xframe.common.core.exception.BusinessException;
import com.xframe.xdal.core.DataBaseAccess;
import com.xframe.xdal.core.model.AKQueryModel;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

//@Service
public class CommandProcService {

//    @Resource
//    CommandRegisterService register;



    static final String ErrorString = "没有找到对应的命令{0}的处理类型,数据类型为{1}";

    static Map<String,SysCommand> commandMap = new HashMap<>();

    public IProcCommandService Create(CommandModel params) {
        String code = params.getCommandCode();
        SysCommand cmd = null;
        if(!commandMap.containsKey(code)) {
            cmd = DataBaseAccess.getInstance().findByAk(AKQueryModel.valueOf(SysCommand.class, code));
            commandMap.put(code,cmd);
        }
        cmd = commandMap.get(code);
        if (cmd == null) {
            throw new BusinessException(ErrorCode.COMMAND_ERROR,
                    MessageFormat.format(ErrorString, params.getCommandCode(), params.getClassName()));
        }
        IProcCommandService proc = cmd.getCommandInstance();
        return proc;
    }
}
